package es.jclm.agri.foragri.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.primefaces.model.LazyDataModel;
import org.springframework.stereotype.Component;
  
@Component
public class TableBean implements Serializable{  
      
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private LazyDataModel<Car> lazyModel;  
  
    private Car selectedCar;  
  
    private List<Car> cars;  
    
    private static String[] colors;
    private static String[] manufacturers;
    private static String[] models;
    static {  
        colors = new String[10];  
        colors[0] = "Black";  
        colors[1] = "White";  
        colors[2] = "Green";  
        colors[3] = "Red";  
        colors[4] = "Blue";  
        colors[5] = "Orange";  
        colors[6] = "Silver";  
        colors[7] = "Yellow";  
        colors[8] = "Brown";  
        colors[9] = "Maroon";  
          
        manufacturers = new String[10];  
        manufacturers[0] = "Mercedes";  
        manufacturers[1] = "BMW";  
        manufacturers[2] = "Volvo";  
        manufacturers[3] = "Audi";  
        manufacturers[4] = "Renault";  
        manufacturers[5] = "Opel";  
        manufacturers[6] = "Volkswagen";  
        manufacturers[7] = "Chrysler";  
        manufacturers[8] = "Ferrari";  
        manufacturers[9] = "Ford";  
        
        models = new String[10];  
        models[0] = "Panda";  
        models[1] = "Toledo";  
        models[2] = "Ibiza";  
        models[3] = "X7";  
        models[4] = "Megane";  
        models[5] = "Testarosa";  
        models[6] = "Focus";  
        models[7] = "Laguna";  
        models[8] = "Punto";  
        models[9] = "Fiesta";  
    }  
  
    public TableBean() {  
        populateRandomCars(getCars(), 50);  
        lazyModel = new LazyCarDataModel(cars);  
    }  
  
    public Car getSelectedCar() {  
        return selectedCar;  
    }  
  
    public void setSelectedCar(Car selectedCar) {  
        this.selectedCar = selectedCar;  
    }  
      
    public LazyDataModel<Car> getLazyModel() {  
        return lazyModel;  
    }  
  
    public List<Car> getCars() {
		if (this.cars == null) {
			this.cars = new ArrayList<Car>();
		}
    	return cars;
	}

	public void setCars(List<Car> cars) {
		this.cars = cars;
	}

	public static String[] getColors() {
		return colors;
	}

	public static void setColors(String[] colors) {
		TableBean.colors = colors;
	}

	public static String[] getManufacturers() {
		return manufacturers;
	}

	public static void setManufacturers(String[] manufacturers) {
		TableBean.manufacturers = manufacturers;
	}

	public static String[] getModels() {
		return models;
	}

	public static void setModels(String[] models) {
		TableBean.models = models;
	}

	public void setLazyModel(LazyDataModel<Car> lazyModel) {
		this.lazyModel = lazyModel;
	}

	private void populateRandomCars(List<Car> list, int size) {  
        for(int i = 0 ; i < size ; i++) {  
            list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));  
        }  
    }  
  
    private String getRandomModel() {  
        return models[(int) (Math.random() * 10)];  
    }  
    private String getRandomColor() {  
        return colors[(int) (Math.random() * 10)];  
    }  
  
    private String getRandomManufacturer() {  
        return manufacturers[(int) (Math.random() * 10)];  
    }  
  
    private int getRandomYear() {  
        return (int) (Math.random() * 50 + 1960);  
    } 
    
    
}  
